
//
//  MineInfoNewViewController.m
//  MJApp
//
//  Created by wangliang on 15/7/22.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoNewViewController.h"

#import "MineInfoNewOneCollectionViewCell.h"
#import "MineInfoNewTwoCollectionViewCell.h"
#import "LoginOutCollectionViewCell.h"

#import "DynamicViewController.h"
#import "MineInfoEditNewViewController.h"

#import "NSDictionary+Category.h"


@interface MineInfoNewViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,HomeCellDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSArray *photos;
@property(nonatomic,strong)NSDictionary *userInfoDic;

@property(nonatomic,strong)NSString *otherID;

@end

@implementation MineInfoNewViewController


- (instancetype)initWithID:(NSString *)userID {
    
    if (self = [super init]) {
        
        self.otherID = userID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self UI];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self obtaionData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)obtaionData {
    
    NSMutableDictionary *dic = [@{@"user_id":[userInfoTool share].ID} mutableCopy];
    if (self.otherID) {
        
        dic[@"user_id"] = self.otherID;
    }
    [WLHttpRequestAfter userCenterInfoWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            self.userInfoDic = [resultDictionary[@"data"][@"user"] nullValueToEmpty];
            self.photos = resultDictionary[@"data"][@"photo"];
            [self.collectionView reloadData];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)UI {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 0, 23, 0);
    flowLayout.minimumLineSpacing = 15;
    CGRect frame = self.view.bounds;
    frame.size.height -= 64;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
    [collectionView registerClass:[MineInfoNewOneCollectionViewCell class] forCellWithReuseIdentifier:@"MineInfoNewOneCollectionViewCell"];
    [collectionView registerClass:[MineInfoNewTwoCollectionViewCell class] forCellWithReuseIdentifier:@"MineInfoNewTwoCollectionViewCell"];
    [collectionView registerClass:[LoginOutCollectionViewCell class] forCellWithReuseIdentifier:@"LoginOutCollectionViewCell"];
    collectionView.backgroundColor = Color_Back_Gray;
    self.collectionView = collectionView;
    
    [self.rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:Color_Top_Text forState:UIControlStateNormal];
    if (self.otherID) {
        
        self.rightBtn.hidden = YES;
    }else {
        
        self.rightBtn.hidden = NO;
    }
}

- (void)clickRightButton {
 
    MineInfoEditNewViewController *editInfoVC = [[MineInfoEditNewViewController alloc] init];
    editInfoVC.title = @"编辑资料";
    [self.navigationController pushViewController:editInfoVC animated:YES];
}


#pragma mark - collectionView Delegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.otherID) {
        
        return 3;
    }else {
        return 4;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = CGSizeZero;
    if (indexPath.row == 0) {
        
        CGFloat rate = 562.0f / 503.0f;
        size.width = IOS_IPHONE_WINDOW_WIDE - 15 * 2;
        size.height = size.width / rate;
    }else if (indexPath.row == 1){
        
        CGFloat rate = 562.0f / (79 * 6 + 119);
        size.width = IOS_IPHONE_WINDOW_WIDE - 15 * 2;
        size.height = size.width / rate;
    }else if (indexPath.row == 3){
        
        CGFloat rate = 562.0f / 95;
        size.width = IOS_IPHONE_WINDOW_WIDE - 15 * 2;
        size.height = size.width / rate;
    }else {
        
        size.width = IOS_IPHONE_WINDOW_WIDE - 15 * 2;
        size.height = 10;
    }
    
    return size;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellID = nil;
    if (indexPath.row == 0) {
        
        cellID = @"MineInfoNewOneCollectionViewCell";
        
    }else if (indexPath.row == 1){
    
        cellID = @"MineInfoNewTwoCollectionViewCell";
    }else if (indexPath.row == 3){
        
        cellID = @"LoginOutCollectionViewCell";
    }else{
        
        cellID = @"CollectionViewCell";
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.row == 2) {
        
        cell.backgroundColor = Color_Back_Gray;
    }
    
    if (indexPath.row == 0) {
        
        MineInfoNewOneCollectionViewCell *theCell = (MineInfoNewOneCollectionViewCell *)cell;
        theCell.infoArray = self.photos;
        theCell.delegate = self;
    }else if (indexPath.row == 1) {
        
        MineInfoNewTwoCollectionViewCell *theCell = (MineInfoNewTwoCollectionViewCell *)cell;
        theCell.delegate = self;
        theCell.infoDic = self.userInfoDic;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 3) {
        
        //退出登录
        [userInfoTool updateInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:K_NOTICE_MINE object:nil];
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"成功退出"];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)homeCell:(id)cell withInfo:(id)info {
    
    
    if ([cell isKindOfClass:[MineInfoNewTwoCollectionViewCell class]]) {
        //查看动态
        
        NSString *userID = [userInfoTool share].ID;
        if (self.otherID) {
            
            userID = self.otherID;
        }
        DynamicViewController *dynamicVC = [[DynamicViewController alloc] initWithDic:@{@"id":userID}];
        dynamicVC.title = @"TA的动态";
        [self.navigationController pushViewController:dynamicVC animated:YES];
    }else {
        //查看相册
        if (self.otherID) {
            
            return;
        }
        MinePhotosViewController *minePhotoVC = [[MinePhotosViewController alloc] initWithOriginArray:self.photos];
        [self.navigationController pushViewController:minePhotoVC animated:YES];
    }
}

@end
